package com.isoftstone.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.isoftstone.pojo.Stipend;
import com.isoftstone.service.StipendService;
@Controller
public class StipendControlle {
	@Autowired
	private StipendService stipendService;
	//录入页面
	@RequestMapping("/addstipend")
	public String addstipend() {
		return "addstipend";
	}
	
	//查看
	@RequestMapping("/stipend")
	public String liststipend(HttpServletRequest req,HttpServletResponse resp) {
		List<Stipend> stipend = stipendService.liststipend();
		req.setAttribute("list", stipend);
		return "liststipend";
	}
	
	
	@RequestMapping("/updatestipend")
	public String updatestipend(HttpServletRequest req,HttpServletResponse resp) {
		String idsrc = req.getParameter("id");
		Long id = Long.parseLong(idsrc);
		Stipend stipend = stipendService.selectstipend(id);
		req.setAttribute("stipend", stipend);
		return "updatestipend";
	}
	
	@RequestMapping("/modifystipend")
	public String modifystipend(Stipend stipend,HttpServletRequest req,HttpServletResponse resp) {
		String action = req.getParameter("action");
		int re = 0;
		if("deletestipend".equals(action)) {
			String idsrc =  req.getParameter("id");
			Long id = Long.parseLong(idsrc);
			stipend.setId(id);
			re = stipendService.modifystipend(id);
		}else if("addstipend".equals(action)){
			stipend.addtol();
			re = stipendService.savesalary(stipend);
		}else{
			String idsrc =  req.getParameter("id");
			Long id = Long.parseLong(idsrc);
			stipend.setId(id);
			System.out.println("id="+id);
			re = stipendService.updatestipend(stipend);
		}
		if(re==1) {
			return liststipend(req,resp);
		}else {
			return "error";
		}
	}
}
